<template>
	<view class="home pageBg">
		<custom-nav-bar title="推荐"></custom-nav-bar>
		<view class="banner">
			<swiper indicator-dots indicator-color="rgba(255, 255, 255, 0.5)" indicator-active-color="#ffffff" circular autoplay>
				<swiper-item v-for="item in bannerList" :key="item._id">
					<!-- 跳转到其它小程序 -->
					<navigator v-if="item.target == 'miniProgram'" 
					:url="item.url" :target="item.target" :app-id="item.appid" class="link">
						<image :src="item.picurl" mode="aspectFill"></image>
					</navigator>
					<navigator v-else :url="`/pages/classlist/classlist?${item.url}`" class="link">
						<image :src="item.picurl" mode="aspectFill"></image>
					</navigator>
				</swiper-item>
			</swiper>
		</view>
		<view class="notice">
			<view class="left">
				<uni-icons type="sound-filled" size="20" color="#28B389"></uni-icons>
				<text class="text">公告</text>
			</view>
			<view class="center">
				<swiper vertical autoplay circular interval="1500" duration="300">
					<swiper-item v-for="item in noticeList" :key="item._id">
						<navigator class="text" :url="`/pages/notice/detail?id=` + item._id">{{item.title}}</navigator>
					</swiper-item>
				</swiper>
			</view>
			<view class="right">
				<uni-icons type="right" size="20" color="#333"></uni-icons>
			</view>
		</view>
		<view class="select">
			<common-title>
				<template #name>
					每日推荐
				</template>
				<template #custom>
					<view class="date">
						<uni-icons type="calendar" size="18" color="#28B389"></uni-icons>
						<uni-dateformat :date="Date.now()" format="dd号"></uni-dateformat>
					</view>
				</template>
			</common-title>
			<view class="content">
				<scroll-view scroll-x>
					<view class="box" v-for="item in randomList" :key="item._id" @click="onPreview(item._id)">
						<image :src="item.smallPicurl" mode="aspectFill"></image>
					</view>
				</scroll-view>
			</view>
		</view>
		<view class="theme">
			<common-title>
				<template #name>专题精选</template>
				<template #custom>
					<navigator url="/pages/classify/classify" open-type="reLaunch" class="more">More+</navigator>
				</template>
			</common-title>
			<view class="content">
				<theme-item v-for="item in classifyList" :key="item._id" :item="item"></theme-item>
				<theme-item :isMore="true"></theme-item>
			</view>
		</view>
	</view>
</template>

<script setup>
import { ref } from 'vue';
import { getBannerListApi, getNoticeListApi, getRandomListApi, getClassifyApi } from '@/api/apis.js';
import { onShareAppMessage, onShareTimeline } from '@dcloudio/uni-app'

const bannerList = ref([])
const noticeList = ref([])
const randomList = ref([])
const classifyList = ref([])

const getClassify = async () => {
	const res = await getClassifyApi({select: true})
	classifyList.value = res.data
}

const getRandomList = async () => {
	const res = await getRandomListApi()
	randomList.value = res.data
}

const getNoticeList = async () => {
	const res = await getNoticeListApi({  })
	noticeList.value = res.data
}

const getBannerList = async () => {
	const res = await getBannerListApi()
	bannerList.value = res.data
}
const onPreview = (id) => {
	uni.setStorageSync("classList", randomList.value)
	uni.navigateTo({
		url: "/pages/preview/preview?id=" + id
	})
}

// 分享给好友
onShareAppMessage(() => {
	return {
		title: '咸虾米壁纸',
		path: '/pages/index/index',
		imageUrl: '/static/images/xxmLogo.png'
	}
})

// 分享给朋友圈
onShareTimeline(() => {
	return {
		title: '咸虾米壁纸',
		imageUrl: '/static/images/xxmLogo.png'
	}
})

getClassify()
getRandomList()
getNoticeList()
getBannerList()
</script>

<style lang="scss" scoped>
.home {
	.banner {
		width: 750rpx;
		padding: 30rpx 0;
		swiper {
			width: 750rpx;
			height: 340rpx;
			&-item {
				width: 100%;
				height: 100%;
				padding: 0 30rpx;
				.link {
					width: 100%;
					height: 100%;
					image {
						width: 100%;
						height: 100%;
						border-radius: 10rpx;
					}
				}
			}
		}
	}
	.notice {
		width: 690rpx;
		height: 80rpx;
		line-height: 80rpx;
		border-radius: 40rpx;
		background-color: #F9F9F9;
		margin: 0 auto;
		display: flex;
		.left {
			width: 140rpx;
			display: flex;
			justify-content: center;
			align-items: center;
			.text {
				font-weight: 600;
				color: $brand-theme-color;
			}
		}
		.center {
			flex: 1;
			swiper {
				height: 100%;
				&-item {
					height: 100%;
					.text {
						overflow: hidden;
						white-space: nowrap;
						text-overflow: ellipsis;
						color: #333;
						font-size: 28rpx;
					}
				}
			}
		}
		.right {
			width: 70rpx;
		}
	}
	.select {
		padding-top: 50rpx;
		.date {
			display: flex;
			align-items: center;
			color: $brand-theme-color;
		}
		.content {
			width: 720rpx;
			margin-left: 30rpx;
			margin-top: 30rpx;
			scroll-view {
				white-space: nowrap;
				.box {
					width: 200rpx;
					height: 430rpx;
					display: inline-block;
					margin-right: 15rpx;
					image {
						width: 100%;
						height: 100%;
						border-radius: 10rpx;
					}
					&:last-child {
						margin-right: 30rpx;
					}
				}
			}
		}
	}
	.theme {
		padding: 50rpx 0;
		.more {
			font-size: 28rpx;
			color: #888;
		}
		.content {
			margin-top: 30rpx;
			padding: 0 30rpx;
			// 网格布局
			display: grid;
			// 间隔
			gap: 15rpx;
			// 每行3个，平均分配
			grid-template-columns: repeat(3, 1fr);
		}
	}
}
</style>
